package com.sanhuo.idms.service;


import com.sanhuo.idms.bo.ClasRemoveBO;
import com.sanhuo.idms.bo.ClasSaveBO;
import com.sanhuo.idms.dao.ClasMapper;
import com.sanhuo.idms.dao.StudentMapper;
import com.sanhuo.idms.entity.Clas;
import com.sanhuo.idms.vo.ClasVo;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClasService extends BaseService<Clas> {

    @MapperWire
    private ClasMapper clasMapper;
    @MapperWire
    private StudentMapper studentMapper;

    @Override
    public BaseMapper<Clas> getBaseMapper() {
        return clasMapper;
    }

    public void save(final UserVo user, ClasSaveBO clasSaveBO) throws IllegalAccessException {
        List<String> clasList = clasMapper.findClasByCollegeIdAndClasGrade(clasSaveBO.getCollegeId(), clasSaveBO.getClasGrade());
        ExceptionUtil.throwException(clasList.contains(clasSaveBO.getClasNum()), "已存在该班级,不能重复添加!");
        Clas clas = new Clas();
        clas.setId(StringUtil.UUID());
        BeanUtil.copyBean(clasSaveBO, clas);
        clas.creatorAndUpdatorInfo(user);
        save(clas);
    }

    public Clas findByNum(String collegeId, String clasGrade, String clasNum) {
        return clasMapper.findBynum(collegeId, clasGrade, clasNum);
    }

    public List<String> findGrade() {
        return clasMapper.findAllGrade();
    }

    public void remove(ClasRemoveBO bo) {
        Clas clas = clasMapper.findByCollegeAndClasGradeAndClasNuM(bo);
        ExceptionUtil.throwException(BeanUtil.isNull(clas), "不存在该班级");
        ExceptionUtil.throwException(studentMapper.countByClasId(clas.getId()) > 0, "该班级存在学生,不可删除!");
        clasMapper.remove(clas.getId());
    }

    public List<ClasVo> findClas(String collegeId, String clasGrade) {
        return clasMapper.findClas(collegeId, clasGrade);
    }

}
